package com.hmy.uaa.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmy.uaa.bo.request.MenuRequest;
import com.hmy.uaa.bo.request.ReqPage;
import com.hmy.uaa.bo.response.RoleMenuResponse;
import com.hmy.uaa.po.Menu;
import com.hmy.uaa.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;
import java.util.List;


@Api(tags = "菜单接口")
@Validated
@RestController
@RequestMapping("/api/menu")
@Slf4j
public class MenuController {

    @Autowired
    private MenuService menuService;


    @PostMapping
    @ApiOperation("新增菜单")
    public void add(@RequestBody @Validated({MenuRequest.Insert.class, Default.class}) MenuRequest menuRequest) {
        log.info(menuRequest.toString());
        menuService.save(menuRequest);
    }

    @PutMapping
    @ApiOperation("修改菜单")
    public void update(@RequestBody @Validated({MenuRequest.Update.class, Default.class}) MenuRequest menuRequest) {
        log.info(menuRequest.toString());
        menuService.update(menuRequest);
    }

    @ApiOperation("删除菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id"),
            @ApiImplicitParam(name = "parentId", value = "父菜单id")
    })
    @DeleteMapping("/{id}/{parentId}")
    public void delete(@PathVariable Long id, @PathVariable Long parentId) {
        log.info("id:{},parentId:{}", id, parentId);
        menuService.delete(id, parentId);

    }


    @ApiOperation("查询菜单列表-分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "菜单名称")
    })
    @GetMapping("/page")
    public Page<Menu> queryPage(ReqPage reqPage,
                                @RequestParam(value = "name", required = false) String name) {

        Page<Menu> page = new Page<>(reqPage.getPageNumber(), reqPage.getPageSize());
        return menuService.queryPage(page, name);
    }

    @ApiOperation("查询菜单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id")
    })
    @GetMapping("/list")
    public List<RoleMenuResponse> queryList(@RequestParam(value = "roleId", required = false) Long roleId) {

        return menuService.queryList(roleId);
    }
}
